package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.ApartmentInfo;
import com.atguigu.lease.model.entity.GraphInfo;
import com.atguigu.lease.model.entity.LabelInfo;
import com.atguigu.lease.model.entity.ViewAppointment;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ViewAppointmentService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.appointment.AppointmentDetailVo;
import com.atguigu.lease.web.app.vo.appointment.AppointmentItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【view_appointment(预约看房信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ViewAppointmentServiceImpl extends ServiceImpl<ViewAppointmentMapper, ViewAppointment>
        implements ViewAppointmentService {
    @Autowired
    private ViewAppointmentMapper viewAppointmentMapper;
    @Autowired
    private ApartmentInfoMapper appointmentDetailVo;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Override
    public List<AppointmentItemVo> queryPersonItemList(long userId) {
        List<AppointmentItemVo> list = viewAppointmentMapper.queryPersonItemList(userId);
        return list;
    }

    @Override
    public AppointmentDetailVo queryDetailById(Long id) {
        //查询AppointmentDetailVo 下的ViewAppointment 部分
        ViewAppointment viewAppointment = viewAppointmentMapper.selectById(id);
        if (ObjectUtils.isEmpty(viewAppointment)) {
            return null;
        }

        //查询AppointmentDetailVo 下的ApartmentItemVo
        //  查询ApartmentItemVo下的apartmentInfo 类属性
        ApartmentInfo apartmentInfo = appointmentDetailVo.queryApartmentInfoById(viewAppointment.getApartmentId());
        //查询ApartmentItemVo 下的公寓labelInfoList
        List<LabelInfo> labelInfoList = labelInfoMapper.querylabel(viewAppointment.getApartmentId());
        //查询查询ApartmentItemVo下的公寓graphInfoList
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,viewAppointment.getApartmentId());
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        List<GraphInfo> graphInfoList = graphInfoMapper.selectList(graphInfoLambdaQueryWrapper);
        //查询ApartmentItemVo 下的 minRent
        BigDecimal minRent = roomInfoMapper.queryMinRent(viewAppointment.getApartmentId());
        //组合ApartmentItemVo
        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
        BeanUtils.copyProperties(apartmentInfo,apartmentItemVo);
        apartmentItemVo.setLabelInfoList(labelInfoList);
        apartmentItemVo.setGraphVoList(graphInfoList);
        apartmentItemVo.setMinRent(minRent);


        //组合AppointmentDetailVo
        AppointmentDetailVo appointmentDetailVo = new AppointmentDetailVo();
        BeanUtils.copyProperties(viewAppointment,appointmentDetailVo);
        appointmentDetailVo.setApartmentItemVo(apartmentItemVo);


        return appointmentDetailVo;
    }
}




